package controllers;

import model.forview.UserChangeModelForView;
import model.forview.UserModelForView;
import model.forview.UsersModelForView;
import model.wrap.user.User;
import model.wrap.user.UserException;
import play.mvc.Controller;
import play.mvc.Result;
import security.Security;

public class UserOption extends Controller
{

    public static Result getPageOption()
    {
	return null;// ok(index.render("Your new application is ready."));
    }

    public static Result administration()
    {
	return null;// ok(index.render("Your new application is ready."));
    }

    public static Result getUserChangePage()
    {
	User user = Security.getUserFromSession();
	UserChangeModelForView modelForView = new UserChangeModelForView(user);
	return ok(views.html.userChange.render(modelForView));
    }
    
    public static Result getUserPage(String userName)
    {
	try
	{
	    UserModelForView modelForView = new UserModelForView(new User(userName));
	    return ok(views.html.user.render(modelForView));
	}
	catch (UserException e)
	{
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	    return badRequest();
	}
    }
    
    public static Result getPageUsers()
    {
	UsersModelForView modelForView = new UsersModelForView();
	return ok(views.html.users.render(modelForView));
    }
    
    public static Result userChange()
    {
	User user = Security.getUserFromSession();
	UserChangeModelForView modelForView = new UserChangeModelForView(form().bindFromRequest().data());
	if(!user.getEmail().equals(modelForView.getUserName()))
	    return badRequest();//TODO
	user.setLastName(modelForView.getLastName());
	user.setFirstName(modelForView.getFirstName());
	user.update();
	return ok(views.html.userChange.render(modelForView));
    }

    public static Result getUserpic(String user)
    {
	try
	{
	    model.wrap.user.User usr = new model.wrap.user.User(user);
	    response().setContentType("img/jpg");
	    response().setHeader("Content-Disposition", "attachment; filename="+String.valueOf(usr.getEmail().hashCode())+".jpg");
	    return ok(usr.getUserpic());
	}
	catch (Exception e)
	{
	    return redirect("/assets/img/userpic.jpg");
	}
    }

}
